package com.copymd;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import org.junit.Test;

import java.io.*;

public class MDtoHTML {

    int count = 0;

    @Test
    public void getHTMLnote() {
        excute("E:\\BaiduNetdiskDownload\\02-Java课V12.5版本172G\\01阶段一：JavaSE基础");
    }


    public void excute(String path) {
        File dirs = new File(path);
        boolean flag = true;
        for (String fileStr : dirs.list()) {
            File file = new File(path + "\\" + fileStr);
            if (file.isDirectory()) {
                excute(path + "\\" + fileStr);
            } else {
                if (fileStr.endsWith("md")) {
                    if (flag) {
                        excuteCMD(path);
                        flag = false;
                        for (String IShtml : new File(path).list()) {
                            count++;
                            if (IShtml.endsWith("html")) {
                                String oldFile = path + "\\" + IShtml;
                                String replace = StrUtil.subSufByLength(oldFile, 40).replace("\\", "-");
                                String newFile = "E:\\笔记归档目录\\" + replace;
                                if (replace.contains("作业") || replace.contains("习题")) {

                                } else {
                                    copyeFile(oldFile, newFile);
                                }
                            }
                        }
                    }

                }
            }

        }
    }

    public void excuteCMD(String path) {
        Runtime runtime = Runtime.getRuntime();
        File file = new File(path);
        try {
            Process exec = runtime.exec("mdtools.bat", null, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void copyeFile(String oldFile, String newFile) {
        try {
            String oldFileTxt = FileUtil.readString(oldFile, "UTF-8");
            String replace = oldFileTxt.replace("!function(e){\"use strict\";function t(){\"complete\"===document.readyState?n():e.addEventListener(\"DOMContentLoaded\",n)}function n(){try{var e=document.querySelectorAll(\"code.hljs\");for(var t in e)e.hasOwnProperty(t)&&r(e[t])}catch(n){console.error(\"LineNumbers error: \",n)}}function r(e){if(\"object\"==typeof e){var t=e.parentNode,n=o(t.textContent);if(n>1){for(var r=\"\",c=0;n>c;c++)r+=c+1+\"\\n\";var l=document.createElement(\"code\");l.className=\"hljs hljs-line-numbers\",l.style[\"float\"]=\"left\",l.textContent=r,t.insertBefore(l,e)}}}function o(e){if(0===e.length)return 0;var t=/\\r\\n|\\r|\\n/g,n=e.match(t);return n=n?n.length:0,e[e.length-1].match(t)||(n+=1),n}\"undefined\"==typeof e.hljs?console.error(\"highlight.js not detected!\"):(e.hljs.initLineNumbersOnLoad=t,e.hljs.lineNumbersBlock=r)}(window);", "");
            FileUtil.writeString(replace, newFile, "UTF-8");

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
